﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XW.Utils;

public static class RandomHelper
{
    /// <summary>
    /// 生成随机数
    /// </summary>
    /// <param name="len"></param>
    /// <returns></returns>
    public static string GetNumber(int len = 4) {
        string result = "";
        var rand = new Random(Guid.NewGuid().GetHashCode());
        for (int i = 0; i < len; i++)
        {
            result += rand.Next(9).ToString();
        }
        return result;
    }
    /// <summary>
    /// 生成随机字符串
    /// </summary>
    /// <param name="len"></param>
    /// <returns></returns>
    public static string GetRandStr(int len= 4) {
        const string data = "abcdefghijkmnopqrstuvwsyz0123456789";
        string result = "";
        var rand = new Random(Guid.NewGuid().GetHashCode());
        for (int i = 0; i < len; i++)
        {
            result += data[rand.Next(data.Length-1)].ToString();
        }
        return result;
    }

    public static int GetRandInt(int start, int end) { 
        var rand = new Random(Guid.NewGuid().GetHashCode());
        return rand.Next(start, end); 
    }

    /// <summary>
    /// 随机时间
    /// </summary>
    /// <param name="dtFr"></param>
    /// <param name="dtTo"></param>
    /// <returns></returns>
    public static DateTime GetRandTime(DateTime dtFr, DateTime dtTo) {
        var rand = new Random(Guid.NewGuid().GetHashCode());
        return rand.NextInt64(dtFr.ToUnixTimestampBySeconds(), dtTo.ToUnixTimestampBySeconds()).ToLocalTimeDateBySeconds();
    }
}
